package cool.taomu.box.kademlia.test

import cool.taomu.toolkit.service.kademlia.service.KRoutingTable
import cool.taomu.toolkit.utils.StringUtils
import org.junit.jupiter.api.Test

class RoutingTableTest {
    
	@Test
	def void xorDistanceTest() {
		var rt = new KRoutingTable();
		val id1 = "1011100100011001100010111001110000001011011010111110111001100101001100001011111110110101110111101010000011101001110000010010101010011000101100011111101011001"
		val id2 = "100001001110110110010001001001101000010100010111110011111001110111011100101111010110111101001011001000010111111100110100000110010100100110110111111100001011110"
        
        var id = "GET:/kademlia/index2/002004"
        var key160 = StringUtils.stringTo160Bit(id);
		// 第一次会计算并缓存
		var result1 = rt.xorDistance(id1, key160)
		println("XOR Distance: " + result1.bitCount)

		// 第二次从缓存中取
		var result2 = rt.xorDistance(id2, key160)
		println("XOR Distance: " + result2.bitCount)
		
		id = "GET:/kademlia/index2/002008"
		key160 = StringUtils.stringTo160Bit(id);
		// 第一次会计算并缓存
        result1 = rt.xorDistance(id1, key160)
        println("XOR Distance: " + result1.bitCount)

        // 第二次从缓存中取
        result2 = rt.xorDistance(id2, key160)
        println("XOR Distance: " + result2.bitCount)
		
	}
	
	@Test
	def void stringTo160BitTest() {
		val id1 = "1101"
		// 第一次会计算并缓存
		val result1 = StringUtils.stringTo160Bit(id1)
		println("XOR Distance: " + result1)

		// 第二次从缓存中取
		val result2 = StringUtils.stringTo160Bit(id1)
		println("XOR Distance (cached): " + result2)
	}
	
}
